package org.dolphin.dao.impl;

import java.util.List;
import org.dolphin.dao.LocaleDao;
import org.dolphin.dao.util.SessionHelper;
import org.dolphin.entity.misc.UserLocale;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository(value = "localDao")
public class LocaleDaoImpl implements LocaleDao {
  @Autowired(required = true)
  private SessionFactory sessionFactory;

  @SuppressWarnings("unchecked")
  @Override
  public List<UserLocale> getUserLocales(List<Order> orders) {
    Session session = SessionHelper.getCurrentSession(sessionFactory);
    Criteria criteria = session.createCriteria(UserLocale.class);
    if (orders != null && !orders.isEmpty()) {
      for (Order order : orders) {
        criteria.addOrder(order);
      }
    }
    List<UserLocale> list = criteria.list();
    return list;
  }

  @Override
  public UserLocale loadUserLocale(Long localeId) {
    Session session = SessionHelper.getCurrentSession(sessionFactory);
    UserLocale userLocale = (UserLocale) session.load(UserLocale.class, localeId);
    return userLocale;
  }
}
